<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 订单支付日志表模型类
 * Created by PhpStorm.
 * User: 66diqiu-li
 * Date: 14-8-1
 * Time: 上午11:09
 */
class Order_Orderpaylog_model extends CI_Model {

    private $sid;
    private $table;
    private $table_order;
    private $table_order_go;
    private $table_site;
    private $table_line;


    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->table = $this->_get_table_name();
        $this->table_order = $this->db->dbprefix("order_info");
        $this->table_order_go = $this->db->dbprefix("order_go_date");
        $this->table_site = $this->db->dbprefix("siteinfo");
        $this->table_line = $this->db->dbprefix("line");
    }

    /**
     * 更新数据
     *
     * @param array $data  数组
     * @param mixed $where 查询条件 array|string
     *
     * @return boolean 返回数据 true/false
     */
    public function edit_row($data, $where)
    {
        $table = $this->_get_table_name();
        if (!empty($where)) $this->db->where($where, null, false);
        return $this->db->update($table, $data);
    }


    /**
     * 根据条件获取订单支付日志
     *
     * @param mixed  $where 查询条件
     * @param string $field 获取字段
     *
     * @return array $data 返回数据
     */
    public function get_list($where = '', $field = '')
    {
        $table = $this->_get_table_name();
        if (!empty($where)) $this->db->where($where, null, false);
        if (!empty($field)) $this->db->select($field);
        $this->db->order_by('add_time', 'DESC');
        $query = $this->db->get($table);
        $data = $query->result_array();
        return $data;
    }

    /**
     * 添加数据
     *
     * @param array $data 数组
     *
     * @return mixed 返回数据 false/[>0]
     */
    public function add_row($data)
    {
        $table = $this->_get_table_name();
        if (is_array($data))
        {
            if ($this->db->insert($table, $data))
            {
                return $this->db->insert_id();
            }
            else
            {
                return false;
            }
        }
        return false;
    }

    /**
     * 查询单条记录
     *
     * @param mixed  $where 查询条件 array|string
     * @param string $field 获取到的字段
     *
     * @return mixed 返回数据
     */
    public function get_row($where, $field = '*')
    {
        $table = $this->_get_table_name();
        if (!empty($where)) $this->db->where($where, null, false);
        if (!empty($field)) $this->db->select($field);
        $query = $this->db->get($table, 1);
        return $query->row_array();
    }

    /**
     * 删除内容
     *
     * @param mixed $where 查询条件 array|string
     *
     * @return bool 返回数据
     */
    public function remove_row($where)
    {
        $table = $this->_get_table_name();
        if (!empty($where)) return $this->db->delete($table, $where);
        return false;
    }

    /**
     * 获取支付金额
     *
     * @param array $where 查询条件
     *
     * @return mixed
     */
    public function get_pay_account($where = array())
    {
        $table = $this->_get_table_name();
        if (!empty($where)) $this->db->where($where, null, false);
        $this->db->select_sum('pay_account');
        $query = $this->db->get($table);
        $row = $query->row_array();
        return $row['pay_account'];
    }

    /**
     * 保存支付信息
     *
     * @param $arg_post
     *
     * @return bool
     */
    function save_pay_account($arg_post)
    {
        $order_id = $arg_post['order_id'];

        $order_row = $this->Orderinfo_model->get_row(array('order_id' => $order_id));

        $pay_log = array(
            'order_id' => $order_id,
            'pay_account' => $arg_post['pay_account'],
            'add_time' => date('Y-m-d H:i:s', now()),
            'action_user' => $this->session->userdata('username'),
            'type' => $arg_post['type'],
            'note' => $arg_post['note'],
            'status' => empty($arg_post['status']) ? 1 : $arg_post['status'],
        );
        $log_id = $this->add_row($pay_log);

        if ($log_id)
        {

            if ($arg_post['type'] == 1) // 游客支付定金
            {
                $order_data = array(
                    'paid_account' => $order_row['paid_account'] + $arg_post['pay_account'],
                    'earnest_paid_account' => $arg_post['pay_account'],
//                    'pay_status' => ($order_row['paid_account'] + $arg_post['pay_account'] >= $order_row['order_account']) ? '3' : '2',
                    'pay_status' => '2',
                );
            }

            if ($arg_post['type'] == 2) // 游客支付尾款
            {
                $order_data = array(
                    'paid_account' => $order_row['paid_account'] + $arg_post['pay_account'],
                    'pay_status' => ($order_row['paid_account'] + $arg_post['pay_account'] >= $order_row['order_account']) ? '3' : '2',
                );
            }

            if ($arg_post['type'] == 3) // 财务支付
            {
                $order_data = array(
                    'paid_account' => $order_row['paid_account'] + $arg_post['pay_account'],
                    'pay_status' => ($order_row['paid_account'] + $arg_post['pay_account'] >= $order_row['order_account']) ? '3' : '2',
                );
            }

            return $this->Orderinfo_model->edit_row($order_data, array('order_id' => $order_id));
        }
        return false;

    }





    /**
     *
     * 支付列表
     *
     * @param $pagesize
     * @param $page
     * @param $where
     */
    function pay_list($pagesize = 20, $page = 1, $data)
    {
        $where = " where b.sid=" . $this->sid;
        if (!empty($data['status']))
        {
            $where .= " and a.status=" . $data['status'];
        }
        if (!empty($data['start_date']))
        {
            $where .= " and a.add_time>='" . $data['start_date'] . "' ";
        }
        if (!empty($data['end_date']))
        {
            $where .= " and a.add_time<='" . $data['end_date'] . "' ";
        }
        if (!empty($data['order_sn']))
        {
            $where .= " and b.order_sn='" . $data['order_sn'] . "'";
        }
        $sql = "select a.*,b.order_sn from " . $this->table . " as a left join " . $this->table_order . " as b on b.order_id=a.order_id " . $where . " order by log_id desc ";
        $start = ($page - 1) * $pagesize;
        $sql .= "limit $start,$pagesize";
        $query = $this->db->query($sql);
        $res = $query->result_array();
        return $res;
    }

    function pay_list_count($data)
    {
        $where = " where b.sid=" . $this->sid;
        if (!empty($data['status']))
        {
            $where .= " and a.status=" . $data['status'];
        }
        if (!empty($data['start_date']))
        {
            $where .= " and a.add_time>='" . $data['start_date'] . "' ";
        }
        if (!empty($data['end_date']))
        {
            $where .= " and a.add_time<='" . $data['end_date'] . "' ";
        }
        if (!empty($data['order_sn']))
        {
            $where .= " and b.order_sn='" . $data['order_sn'] . "'";
        }

        $sql = "select count(a.log_id) as num from " . $this->table . " as a left join " . $this->table_order . " as b on b.order_id=a.order_id " . $where . " order by log_id desc ";
        $query = $this->db->query($sql);
        $res = $query->row_array();
        return $res['num'];
    }

    public function pay_list_supplier($pagesize = 20, $page = 1,
        $where = array())
    {
        $admin_id = $this->session->userdata('admin_user_id');//供应商对应自己的订单

        $where_str = " and l.admin_id=$admin_id ";
        if (!empty($where['status']))
        {
            $where_str .= " and a.status=" . $where['status'];
        }
        if (!empty($where['start_date']))
        {
            $where_str .= " and a.add_time>='" . $where['start_date'] . "' ";
        }
        if (!empty($where['end_date']))
        {
            $where_str .= " and a.add_time<='" . $where['end_date'] . "' ";
        }
        if (!empty($where['order_sn']))
        {
            $where_str .= " and o.order_sn='" . $where['order_sn'] . "'";
        }
        $sql = " select a.*,s.name,s.url,o.order_sn from $this->table as a  "
            . " left join $this->table_order as o on o.order_id=a.order_id "
            . " left join $this->table_order_go  as g on g.order_id=o.order_id "
            . " left join $this->table_line  as l on l.line_id=g.line_id "
            . " left join $this->table_site as s on s.sid=a.sid "
            . " where a.type=2 $where_str ";
        $query_count = $this->db->query($sql);
        $count = $query_count->num_rows();
        $start = $pagesize * ($page - 1);
        $sql .= " limit $start,$pagesize";
        $query = $this->db->query($sql);
        $list = $query->result_array();
        return array('count' => $count, 'list' => $list);
    }

    /**
     * 获取数据表名称
     */
    private function _get_table_name()
    {
        $table = $this->db->dbprefix("order_pay_log");
        return $table;
    }

    public function get_order_id_by_log_id($log_id)
    {
        $log_id = intval($log_id);
        $sql = "select order_id from " . $this->table . " where log_id=$log_id ";
        $query = $this->db->query($sql);
        $res = $query->row_array();
        return $res['order_id'];
    }
}